package com.okilo.online.app.act

import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import com.okilo.online.app.BuildConfig
import com.okilo.online.app.R
import com.okilo.online.app.common.OkiloComAct
import com.okilo.online.app.databinding.OkiloActSettingBinding
import com.okilo.online.app.event.OkiloEventPointType
import com.okilo.online.app.tools.OkiloTitleBarUtil
import com.okilo.online.app.tools.apt.OkiloOnSingleClickListener
import com.okilo.online.app.tools.helper.OkiloConfigCustomerHp
import com.okilo.online.app.tools.helper.OkiloUpdateHp
import com.okilo.online.app.tools.shared.OkiloSharedHp
import com.okilo.online.app.dialog.OkiloClearDataDlog
import com.okilo.online.app.pet.OkiloUserPet
import com.okilo.online.app.pet.view.IOkiloUser
import org.json.JSONObject

/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2024/11/27 16:16
 * @Annotation：设置
 */
class OkiloSettingAct: OkiloComAct(), IOkiloUser.IOkiloDeleteUserData {
    var _binding: OkiloActSettingBinding? = null

    //用户Presenter
    private var userPet: OkiloUserPet? = null
    private var deleteDataAfterDate = ""

    private var updateHelper: OkiloUpdateHp? = null
    private val updateLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
        if (result.resultCode == RESULT_OK) {
            //更新成功

        } else {

        }
    }

    override val pageName: String = "UserSetting_Page"

    override fun createOkiloContentView(): View? {
        _binding = OkiloActSettingBinding.inflate(layoutInflater)
        return _binding?.root
    }

    override fun initializeOkiloViews() {
        getTitleBarManager()
            .setTitleBarVisibility(View.VISIBLE)
            .setTitle(getString(R.string.okilo_tab_me_setting))
            .setTitleColor(getColor(R.color.okilo_color_black))
            .setRightButtonIcon(R.mipmap.okilo_root_customer_icon)
            .setBackButtonIcon(R.mipmap.okilo_root_back_icon)
            .setRootBackgroundColor(getColor(R.color.okilo_color_ffeff1f3))
        OkiloTitleBarUtil.setStatusBarLightMode(this, true)
        getTitleBarManager().setRootBackgroundColor(getColor(R.color.okilo_color_white))
    }

    override fun setupOkiloEventListeners() {
        super.setupOkiloEventListeners()
        //更新版本
        _binding?.okiloSettingButtonUpdate?.setOnClickListener {
            addEventPointRequestAction(OkiloEventPointType.OKILO_CLICK_TRACK.UPDATE_VERSION_CHECK,pageName)
            //是否有新版本
            val isUpdate = OkiloConfigCustomerHp.iOkiloReleaseUpdate()
            if (!isUpdate){
                showOkiloToast(getString(R.string.okilo_app_version_update_no_version))
                return@setOnClickListener
            }
            if (updateHelper == null){
                updateHelper = OkiloUpdateHp(activity = this, forceUpdate = false, updateLauncher)
            }
            updateHelper?.checkAndStartUpdate { }
        }

        //清空用户数据
        _binding?.okiloSettingButtonDelete?.setOnClickListener(object : OkiloOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                addEventPointRequestAction(OkiloEventPointType.OKILO_CLICK_TRACK.USER_DATA_CLEAR,pageName)
                sOkiloClearDataDialog(deleteDataAfterDate)
            }
        })
    }

    override fun loadOkiloData() {
        val binding = _binding ?: return
        //获取版本名称
        binding.okiloSettingVersionText.text = String.format(getString(R.string.okilo_setting_version), BuildConfig.VERSION_NAME)

        //是否有新版本
        val isUpdate = OkiloConfigCustomerHp.iOkiloReleaseUpdate()
        if (isUpdate){
            binding.okiloSettingUpdatePointView.visibility = View.VISIBLE
        } else {
            binding.okiloSettingUpdatePointView.visibility = View.GONE
        }

        binding.okiloSettingButtonDelete.visibility = View.INVISIBLE
        if (userPet == null){
            userPet = OkiloUserPet(this)
        }
        showOkiloLoading()
        userPet?.gOkiloUserClearDataRequest(true,this)
    }

    override fun onResume() {
        super.onResume()
        updateHelper?.resumeIfInProgress()
    }

    /**
     * 清空用户数据dialog
     */
    private fun sOkiloClearDataDialog(afterDate: String){
        val clearDialog =
            OkiloClearDataDlog(this, pageName, object : OkiloClearDataDlog.IDialogCallback {
                override fun onUserDataClearConfirmClick() {
                    showOkiloLoading()
                    userPet?.gOkiloUserClearDataRequest(false, this@OkiloSettingAct)
                }
            })
        clearDialog.setCancelable(true)
        clearDialog.setCanceledOnTouchOutside(true)
        clearDialog.show()
        clearDialog.sOkiloUserDataDeleteInfo(OkiloConfigCustomerHp.gOkiloDeleteUserDataDesc(),afterDate)
    }

    /**
     * 是否显示清除用户数据按钮
     */
    override fun sOkiloShowDeleteUserData(data: JSONObject) {
        if (data.optString("showDropButton","NO") == "YES"){
            _binding?.okiloSettingButtonDelete?.visibility = View.VISIBLE
            deleteDataAfterDate = data.optString("againDate")
        } else {
            _binding?.okiloSettingButtonDelete?.visibility = View.INVISIBLE
        }
    }

    /**
     * 清除用户数据成功
     */
    override fun sOkiloDeleteUserDataSuccess() {
        hideOkiloLoading()
        //退出登录，需要移除用户保存的信息
        OkiloSharedHp.onOkiloAppOut()
        onIntent(OkiloUserLoginAct::class.java)
    }

    override fun onDestroy() {
        super.onDestroy()
        userPet = null
        _binding = null
    }
}